/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.Desktop.Utils;

import com.kylingroup.Desktop.Bean.Bean_Plugin;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author kylin
 */
public class PluginWriter {
    private File xmlfile=null;
    private Document document;
    
    public PluginWriter(){}
    
    public void setXml(File xmlfile){
        this.xmlfile=xmlfile;
    }
    
    public File create(String path){
        xmlfile=new File(path);
        document=DocumentHelper.createDocument();
        Element root=document.addElement("kgplugins");
        document.setRootElement(root);
        write();
        return xmlfile;
    }
    
    public Element getRoot(){
        SAXReader saxReader=new SAXReader();   
        try {
            document=saxReader.read(xmlfile);
        } catch (DocumentException ex) {
            Logger.getLogger(PluginWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
        Element root=document.getRootElement();
        return root;
    }
    
    public void addElement(Element father,Bean_Plugin plug){
        Element plugin=father.addElement("plugin");
        plugin.setAttributeValue("id", String.valueOf(plug.getId()));
        plugin.setAttributeValue("autoload", String.valueOf(plug.isAutoload()));
        
        Element name=plugin.addElement("name");
        name.setText(plug.getName());
        
        
        Element clazz=plugin.addElement("class");
        clazz.setText(plug.getClazz());
        
        Element category=plugin.addElement("category");
        category.setText(plug.getCategory());
        
        Element version=plugin.addElement("version");
        version.setText(plug.getVersion());
        
        Element description=plugin.addElement("description");
        description.setText(plug.getDescription());
        
        Element author=plugin.addElement("author");
        author.setText(plug.getAuthor());
        
        Element email=plugin.addElement("email");
        email.setText(plug.getEmail());
    }
    
    public void write(){
        try{
            OutputFormat outFmt = new OutputFormat("\t", true);  
            outFmt.setEncoding("UTF-8");
            XMLWriter output = new XMLWriter(new FileOutputStream(xmlfile),outFmt); 
            output.write(document);
            output.close();
	}catch(IOException e){
            e.printStackTrace();
	}
    }
    
    public List getPlugins(Element root){
        List<Bean_Plugin> list=new ArrayList<Bean_Plugin>();
        
        Iterator itorroot=root.elementIterator();
        //.elementIterator("Elemennt-font");//获取Elemennt-font节点
	while(itorroot.hasNext()){
            Element plugin=(Element)itorroot.next();
            Iterator itorplugin=plugin.elementIterator();
            Bean_Plugin p=new Bean_Plugin();
            List<Attribute> listAttr=plugin.attributes();
            for(Attribute attr:listAttr){
                if(attr.getName().equals("id")){
                    p.setId(Integer.parseInt(attr.getValue()));
                }else if(attr.getName().equals("autoload")){
                    p.setAutoload(Boolean.parseBoolean(attr.getValue()));
                }
            }  
            
            while(itorplugin.hasNext()){
                Element element=(Element)itorplugin.next();
                if(element.getName().equals("name")){
                    p.setName(element.getText());
                }else if(element.getName().equals("class")){
                    p.setClazz(element.getText());
                }else if(element.getName().equals("category")){
                    p.setCategory(element.getText());
                }else if(element.getName().equals("version")){
                    p.setVersion(element.getText());
                }else if(element.getName().equals("description")){
                    p.setDescription(element.getText());
                }else if(element.getName().equals("author")){
                    p.setAuthor(element.getText());
                }else if(element.getName().equals("email")){
                    p.setEmail(element.getText());
                }
            }
            list.add(p);
        }
        
        return list;
    }
}
